package com.relax.netty.secondexample;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.UUID;

public class MyServerHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println("当前线程:"+Thread.currentThread().getName());
        System.out.println("ctx"+ctx+",channel:"+ctx.channel()+",pipline:"+ctx.channel().pipeline());
        final Channel channel = ctx.channel();
        System.out.println(ctx.channel().remoteAddress() + "," + msg);
        ctx.channel().writeAndFlush("ok,i got: " + UUID.randomUUID().toString());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        //如果出现异常,关闭ctx
        ctx.close();
    }
}
